{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 明日学长 给师弟师妹们留下实践经验\n",
    "## 项目：实现Azure face人脸验证\n",
    "\n",
    "- 选用Azure Face - Find Similar API，实现人脸对比，具体实现如下：\n",
    "- Create facelist 创建并命名好你的facelist\n",
    "- Add face 添加至少10张face作为对比人脸数据\n",
    "- get查看facelists中的信息\n",
    "- Detect 检测出你需要的人脸并获取faceid\n",
    "- Find similars 返回相似置信度\n",
    "- 提交HTML档，命名为【姓名-fscelist名-学号】"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style>\n",
       "/* 本电子讲义使用之CSS */\n",
       "div.code_cell {\n",
       "    background-color: #e5f1fe;\n",
       "}\n",
       "div.cell.selected {\n",
       "    background-color: #effee2;\n",
       "    font-size: 2rem;\n",
       "    line-height: 2.4rem;\n",
       "}\n",
       "div.cell.selected .rendered_html table {\n",
       "    font-size: 2rem !important;\n",
       "    line-height: 2.4rem !important;\n",
       "}\n",
       ".rendered_html pre code {\n",
       "    background-color: #C4E4ff;   \n",
       "    padding: 2px 25px;\n",
       "}\n",
       ".rendered_html pre {\n",
       "    background-color: #99c9ff;\n",
       "}\n",
       "div.code_cell .CodeMirror {\n",
       "    font-size: 2rem !important;\n",
       "    line-height: 2.4rem !important;\n",
       "}\n",
       ".rendered_html img, .rendered_html svg {\n",
       "#     max-width: 20%;\n",
       "#     max-height: 20%;\n",
       "    height: 200px;\n",
       "    width: 200px;\n",
       "    overflow: hidden;\n",
       "#     float: center;\n",
       "    float:left;\n",
       "    padding-left:20px;\n",
       "    #border:1px solid #336699;\n",
       "}\n",
       "/* Gradient transparent - color - transparent */\n",
       "hr {\n",
       "    border: 0;\n",
       "    border-bottom: 1px dashed #ccc;\n",
       "}\n",
       ".emoticon{\n",
       "    font-size: 5rem;\n",
       "    line-height: 4.4rem;\n",
       "    text-align: center;\n",
       "    vertical-align: middle;\n",
       "}\n",
       "\n",
       "</style>\n"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%%html\n",
    "<style>\n",
    "/* 本电子讲义使用之CSS */\n",
    "div.code_cell {\n",
    "    background-color: #e5f1fe;\n",
    "}\n",
    "div.cell.selected {\n",
    "    background-color: #effee2;\n",
    "    font-size: 2rem;\n",
    "    line-height: 2.4rem;\n",
    "}\n",
    "div.cell.selected .rendered_html table {\n",
    "    font-size: 2rem !important;\n",
    "    line-height: 2.4rem !important;\n",
    "}\n",
    ".rendered_html pre code {\n",
    "    background-color: #C4E4ff;   \n",
    "    padding: 2px 25px;\n",
    "}\n",
    ".rendered_html pre {\n",
    "    background-color: #99c9ff;\n",
    "}\n",
    "div.code_cell .CodeMirror {\n",
    "    font-size: 2rem !important;\n",
    "    line-height: 2.4rem !important;\n",
    "}\n",
    ".rendered_html img, .rendered_html svg {\n",
    "#     max-width: 20%;\n",
    "#     max-height: 20%;\n",
    "    height: 200px;\n",
    "    width: 200px;\n",
    "    overflow: hidden;\n",
    "#     float: center;\n",
    "    float:left;\n",
    "    padding-left:20px;\n",
    "    #border:1px solid #336699;\n",
    "}\n",
    "/* Gradient transparent - color - transparent */\n",
    "hr {\n",
    "    border: 0;\n",
    "    border-bottom: 1px dashed #ccc;\n",
    "}\n",
    ".emoticon{\n",
    "    font-size: 5rem;\n",
    "    line-height: 4.4rem;\n",
    "    text-align: center;\n",
    "    vertical-align: middle;\n",
    "}\n",
    "\n",
    "</style>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 我最像谁？\n",
    "- 选取20春_pm_c班部分同学进行对比，检测我最像谁\n",
    "> 想靠脸缘进行组队？想看看谁跟你长得最像？本次，我们通过微软认知服务为大家演示如何用代码找到最像你的那个人\n",
    "用代码论证，我是谁？我最像谁？谁跟我最有脸缘！"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 开始之前\n",
    "- 在开始之前先了解以下文档吧\n",
    "- 了解什么是[视觉认知服务](https://docs.microsoft.com/zh-cn/azure/cognitive-services/welcome)\n",
    "- [认知服务api文档](https://westus.dev.cognitive.microsoft.com/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395236)\n",
    "\n",
    "## 寻找相似的脸\n",
    "给定查询人脸的faceId，可以从faceId数组，人脸列表或大型人脸列表中搜索外观相似的人脸。faceId数组包含Face-Detect创建的 面孔，这些面孔将在创建后24小时过期。由FaceList创建 “ faceListId” -创建包含不会过期的persistedFaceId。然后，LargeFaceList创建一个“ largeFaceListId” -创建包含也不会过期的persistedFaceIds。根据输入，返回的相似面孔列表包含按相似度排序的faceId或persistedFaceId。\n",
    "\n",
    "查找类似有两种工作模式，“ matchPerson”和“ matchFace”。“ matchPerson”是默认模式，它尝试通过使用内部同一个人阈值来查找同一个人的面孔。查找已知人的其他照片很有用。请注意，如果没有人通过内部阈值，将返回一个空列表。“ matchFace”模式会忽略同一人的阈值，即使相似度很低，无论如何仍会返回排名相似的面孔。它可以用于搜索名人面孔的情况。\n",
    "\n",
    "与查询面部的faceId相关联的“ recognitionModel”应与目标faceId数组，面部列表或大型面部列表所使用的“ recognitionModel”相同。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 注册一个免费账号吧\n",
    "- [七天免费试用](https://azure.microsoft.com/zh-cn/try/cognitive-services/?api=face-api)\n",
    "- [免费账号](https://azure.microsoft.com/zh-cn/free/?WT.mc_id=A261C142F)\n",
    "- [申请education github获得免费账号](https://education.github.com/pack/offers)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 建列表\n",
    "> 在开始检测之前，我们需要构建一个列表用于装取我们需要进行对比的人脸\n",
    "- [建列表api文档](https://westus.dev.cognitive.microsoft.com/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f3039524b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "#先写一下通用的东西\n",
    "import requests #导入requests模块\n",
    "import json\n",
    "create_facelists_url = \"https://api-hjq.cognitiveservices.azure.com/face/v1.0/facelists/{faceListId}\" \n",
    "subscription_key = \"f212f71663134b73bdec4a5a9aece2f0\"\n",
    "assert subscription_key\n",
    "headers = {\n",
    "    # Request headers\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': subscription_key,\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1、create 创建一个装相片的列表\n",
    "data = {\n",
    "    \"name\": \"list_pm_c\",\n",
    "    \"userData\": \"我的同学们\",\n",
    "    \"recognitionModel\": \"recognition_02\"\n",
    "}\n",
    "params = {\n",
    "    # Request parameters    \n",
    "   \n",
    "    \"faceListId\":\"who_you\"\n",
    "    \n",
    "#     'detectionModel': 'detection_01',\n",
    "}\n",
    "r_create = requests.put(create_facelists_url,headers=headers,params=params,json=data)\n",
    "# 请求成功，返回空字符串\n",
    "r_create.status_code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b''"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_create.content"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 存个脸\n",
    "> 我们通过上面的步骤建好了一个脸的列表，接下来我们要给这个列表添加脸了！把我们想要对比的脸存进列表吧\n",
    "- [添加脸api文档](https://westus.dev.cognitive.microsoft.com/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395250)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "#先加一张脸试试\n",
    "# 2、Add face\n",
    "add_face_url =\"https://api-hjq.cognitiveservices.azure.com/face/v1.0/facelists/{faceListId}/persistedFaces\"\n",
    "\n",
    "assert subscription_key\n",
    "headers = {\n",
    "    # Request headers\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': subscription_key,\n",
    "}\n",
    "img_url = \"http://huangjieqi.gitee.io/picture_storage/EdisonQXF.jpg\"\n",
    "\n",
    "params_add_face={\n",
    "    \"faceListId\":\"who_you\",\n",
    "    \"userData\":\"丘小峰\"\n",
    "}\n",
    "\n",
    "r_add_face = requests.post(add_face_url,headers=headers,params=params_add_face,json={\"url\":img_url})\n",
    "# r = requests.post(add_face, headers=headers,params = params,json={\"url\": add_face_image})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'persistedFaceId': '598f6ecd-049e-45bf-916b-e011e79700e7'}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_add_face.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 封装成函数\n",
    "> 我们要添加多张脸，但是为了减少代码量，我们可以把代码封装成函数，避免每次都要写一大堆代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 封装成函数方便添加图片\n",
    "def AddFace(img_url=str,userData=str):\n",
    "    add_face_url =\"https://api-hjq.cognitiveservices.azure.com/face/v1.0/facelists/{faceListId}/persistedFaces\"\n",
    "    assert subscription_key\n",
    "    headers = {\n",
    "        # Request headers\n",
    "        'Content-Type': 'application/json',\n",
    "        'Ocp-Apim-Subscription-Key': subscription_key,\n",
    "    }\n",
    "    img_url = img_url\n",
    "\n",
    "    params_add_face={\n",
    "        \"faceListId\":\"who_you\",\n",
    "        \"userData\":userData\n",
    "    }\n",
    "    r_add_face = requests.post(add_face_url,headers=headers,params=params_add_face,json={\"url\":img_url})\n",
    "    return r_add_face.status_code#返回出状态码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 顺便把查看列表的也给封装了\n",
    "# Get facelist\n",
    "def GetFacelist():\n",
    "    get_facelist_url = \"https://api-hjq.cognitiveservices.azure.com/face/v1.0/facelists/{faceListId}\"\n",
    "    data = {\n",
    "        \"faceListId\":\"who_you\",\n",
    "    }\n",
    "    r_get_facelist = requests.get(get_facelist_url,headers=headers,params=data)\n",
    "    return r_get_facelist.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'persistedFaces': [{'persistedFaceId': '598f6ecd-049e-45bf-916b-e011e79700e7',\n",
       "   'userData': '丘小峰'},\n",
       "  {'persistedFaceId': '2f658068-ef61-4708-b91c-d05d4e1e0cc5',\n",
       "   'userData': '郑梓轩'},\n",
       "  {'persistedFaceId': 'ceff8e23-e304-4b96-bea0-2f33adfa1b62',\n",
       "   'userData': '郑晓萍'},\n",
       "  {'persistedFaceId': 'b41a0ba7-e29c-46e6-8c27-02ac6b1ddbfe',\n",
       "   'userData': '丘天惠'},\n",
       "  {'persistedFaceId': '7addfab0-e8b4-4ae7-bfea-0d0b0908feba',\n",
       "   'userData': '林嘉茵'},\n",
       "  {'persistedFaceId': '76c3c12f-a25d-439c-879e-9a8f43a29370',\n",
       "   'userData': '汤玲萍'},\n",
       "  {'persistedFaceId': '7bbdc1b8-6322-4a9a-9246-5049f0cd24e0',\n",
       "   'userData': '曾雯燕'},\n",
       "  {'persistedFaceId': '5bd52697-3745-44f7-9907-090d871c9692',\n",
       "   'userData': '谢依希'},\n",
       "  {'persistedFaceId': 'b6638cb0-83a1-41d0-9e8a-8dd3ce2f7dbb',\n",
       "   'userData': '杨悦聪'},\n",
       "  {'persistedFaceId': '41b0c22b-ea49-418a-9d89-4661d771de0a',\n",
       "   'userData': '周雨'},\n",
       "  {'persistedFaceId': 'caf93389-bd39-43aa-8ea8-a43797cb99d1',\n",
       "   'userData': '刘瑜鹏'},\n",
       "  {'persistedFaceId': '6245988d-0e6c-4307-949c-3b4e42a9f23b',\n",
       "   'userData': '陈嘉仪'},\n",
       "  {'persistedFaceId': 'b4e21332-4ac6-46e9-bce9-7cd8eb81fd98',\n",
       "   'userData': '徐旖芊'},\n",
       "  {'persistedFaceId': '6c27182b-fabd-4a8f-a477-3a8185194a5b',\n",
       "   'userData': '刘心如'},\n",
       "  {'persistedFaceId': 'b58ac686-aa4c-445a-8114-95a7cc044e10',\n",
       "   'userData': '刘宇'},\n",
       "  {'persistedFaceId': '585b5548-2693-44f8-a8b2-2bc177fe5ba6',\n",
       "   'userData': '李婷'},\n",
       "  {'persistedFaceId': 'feec5a9a-e7d4-46c5-94cb-28377778d697',\n",
       "   'userData': '黄智毅'},\n",
       "  {'persistedFaceId': '50d1ff59-4e82-488d-b878-10549d1b97a8',\n",
       "   'userData': '黄慧文'},\n",
       "  {'persistedFaceId': 'ea130ca7-7817-46d0-a5eb-0625e69bb214',\n",
       "   'userData': '张铭睿'},\n",
       "  {'persistedFaceId': '166c9164-add0-45ed-93f3-591f9d5d261a',\n",
       "   'userData': '陈婷'},\n",
       "  {'persistedFaceId': 'd9a0952f-9991-4196-950e-55c337b5838b',\n",
       "   'userData': '洪可凡'},\n",
       "  {'persistedFaceId': 'd66b3761-dc9b-4dfb-a19a-aaf06c450a24',\n",
       "   'userData': '卢继志'},\n",
       "  {'persistedFaceId': '27be3057-d252-4294-98ea-d55471c519d3',\n",
       "   'userData': '张梓乐'}],\n",
       " 'faceListId': 'who_you',\n",
       " 'name': 'list_pm_c',\n",
       " 'userData': '我的同学们'}"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "GetFacelist()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/EdisonQXF.jpg\",\"郑梓轩\")#可以先用一个试试水"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 添加大量的图片\n",
    "> 添加一张图片似乎不够，我们需要添加大量图片进行对比，这里也可以用for循环自动跑程序，自己尝试一下吧"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](http://huangjieqi.gitee.io/picture_storage/EdisonQXF.jpg)\n",
    "![](http://huangjieqi.gitee.io/picture_storage/Autumnhui.jpg)\n",
    "![](http://huangjieqi.gitee.io/picture_storage/L-Tony-info.jpg)\n",
    "![](http://huangjieqi.gitee.io/picture_storage/TLINGP.jpg)\n",
    "![](http://huangjieqi.gitee.io/picture_storage/WenYanZeng.jpg)\n",
    "![](http://huangjieqi.gitee.io/picture_storage/XIEIC.jpg)\n",
    "![](http://huangjieqi.gitee.io/picture_storage/YuecongYang.png)\n",
    "![](http://huangjieqi.gitee.io/picture_storage/Zoezhouyu.jpg)\n",
    "![](http://huangjieqi.gitee.io/picture_storage/crayon-heimi.jpg)\n",
    "![](http://huangjieqi.gitee.io/picture_storage/jiayichen.jpg)\n",
    "![](http://huangjieqi.gitee.io/picture_storage/kg2000.jpg)\n",
    "![](http://huangjieqi.gitee.io/picture_storage/liuxinrujiayou.jpg)\n",
    "![](http://huangjieqi.gitee.io/picture_storage/liuyu19.png)\n",
    "![](http://huangjieqi.gitee.io/picture_storage/ltco.jpg)\n",
    "![](http://huangjieqi.gitee.io/picture_storage/lucaszy.jpg)\n",
    "![](http://huangjieqi.gitee.io/picture_storage/pingzi0211.jpg)\n",
    "![](http://huangjieqi.gitee.io/picture_storage/shmimy-cn.jpg)\n",
    "![](http://huangjieqi.gitee.io/picture_storage/yichenting.jpg)\n",
    "![](http://huangjieqi.gitee.io/picture_storage/coco022.jpg)\n",
    "![](http://huangjieqi.gitee.io/picture_storage/lujizhi.png)\n",
    "![](http://huangjieqi.gitee.io/picture_storage/zzlhyy.jpg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/zxp.jpg\",\"郑晓萍\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/Autumnhui.jpg\",\"丘天惠\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/L-Tony-info.jpg\",\"林嘉茵\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/TLINGP.jpg\",\"汤玲萍\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/WenYanZeng.jpg\",\"曾雯燕\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/XIEIC.jpg\",\"谢依希\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/YuecongYang.png\",\"杨悦聪\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/Zoezhouyu.jpg\",\"周雨\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/crayon-heimi.jpg\",\"刘瑜鹏\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/jiayichen.jpg\",\"陈嘉仪\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/kg2000.jpg\",\"徐旖芊\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/liuxinrujiayou.jpg\",\"刘心如\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/liuyu19.png\",\"刘宇\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/ltco.jpg\",\"李婷\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/lucaszy.jpg\",\"黄智毅\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/pingzi0211.jpg\",\"黄慧文\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/shmimy-cn.jpg\",\"张铭睿\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/yichenting.jpg\",\"陈婷\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/coco022.jpg\",\"洪可凡\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/lujizhi.png\",\"卢继志\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/zzlhyy.jpg\",\"张梓乐\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 遇到400异常！别慌！\n",
    "> 这是你的图片不够清晰导致的，换一张图片吧"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "400"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#当你的图片不够清晰或者识别不出来的时候\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/Michael07.jpg\",\"钟振升\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/Vicky923.jpg\",\"黄苑婷\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 测个脸\n",
    "> 现在我们要检测一个脸出来，与列表中的脸进行对比。\n",
    "- [检测脸文档](https://westus.dev.cognitive.microsoft.com/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395236)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'faceId': 'e669a859-9d6b-4f80-8b72-711f4942bdd7',\n",
       "  'faceRectangle': {'top': 264, 'left': 207, 'width': 277, 'height': 277},\n",
       "  'faceAttributes': {'smile': 0.0,\n",
       "   'headPose': {'pitch': 1.7, 'roll': -19.8, 'yaw': -13.9},\n",
       "   'gender': 'male',\n",
       "   'age': 20.0,\n",
       "   'facialHair': {'moustache': 0.1, 'beard': 0.1, 'sideburns': 0.1},\n",
       "   'glasses': 'NoGlasses',\n",
       "   'emotion': {'anger': 0.0,\n",
       "    'contempt': 0.002,\n",
       "    'disgust': 0.0,\n",
       "    'fear': 0.0,\n",
       "    'happiness': 0.0,\n",
       "    'neutral': 0.969,\n",
       "    'sadness': 0.029,\n",
       "    'surprise': 0.0},\n",
       "   'blur': {'blurLevel': 'medium', 'value': 0.44},\n",
       "   'exposure': {'exposureLevel': 'goodExposure', 'value': 0.68},\n",
       "   'noise': {'noiseLevel': 'medium', 'value': 0.39},\n",
       "   'makeup': {'eyeMakeup': False, 'lipMakeup': False},\n",
       "   'accessories': [],\n",
       "   'occlusion': {'foreheadOccluded': False,\n",
       "    'eyeOccluded': False,\n",
       "    'mouthOccluded': False},\n",
       "   'hair': {'bald': 0.18,\n",
       "    'invisible': False,\n",
       "    'hairColor': [{'color': 'brown', 'confidence': 0.97},\n",
       "     {'color': 'black', 'confidence': 0.9},\n",
       "     {'color': 'red', 'confidence': 0.38},\n",
       "     {'color': 'other', 'confidence': 0.23},\n",
       "     {'color': 'blond', 'confidence': 0.15},\n",
       "     {'color': 'gray', 'confidence': 0.09}]}}}]"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 3、检测人脸的id\n",
    "# replace <My Endpoint String> with the string from your endpoint URL\n",
    "face_api_url = 'https://api-hjq.cognitiveservices.azure.com/face/v1.0/detect'\n",
    "\n",
    "# 请求正文\n",
    "image_url = 'http://huangjieqi.gitee.io/picture_storage/hjq.jpg'\n",
    "\n",
    "headers = {'Ocp-Apim-Subscription-Key': subscription_key}\n",
    "\n",
    "# 请求参数\n",
    "params = {\n",
    "    'returnFaceId': 'true',\n",
    "    'returnFaceLandmarks': 'false',\n",
    "    # 选择model\n",
    "    'recognitionModel':'recognition_02',\n",
    "    'detectionModel':'detection_01',\n",
    "    # 可选参数,请仔细阅读API文档\n",
    "    'returnFaceAttributes': 'age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,noise',\n",
    "}\n",
    "\n",
    "response = requests.post(face_api_url, params=params,\n",
    "                         headers=headers, json={\"url\": image_url})\n",
    "# json.dumps 将json--->字符串\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "<img src=\"http://huangjieqi.gitee.io/picture_storage/hjq.jpg\" width=\"50%\" height=\"50%\">"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "我的脸 = response.json()[0][\"faceId\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 找相似\n",
    "> 最后一步，将我的脸与列表中的脸进行对比，得到与你有一。。像的脸吧\n",
    "- [查找相似api文档](https://westus.dev.cognitive.microsoft.com/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395237)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "#4、Find similars\n",
    "\n",
    "findsimilars_url = \"https://api-hjq.cognitiveservices.azure.com/face/v1.0/findsimilars\"\n",
    "\n",
    "# 请求正文 faceId需要先检测一张照片获取\n",
    "data_findsimilars = {\n",
    "    \"faceId\":我的脸,\n",
    "    \"faceListId\":\"who_you\",\n",
    "    \"maxNumOfCandidatesReturned\": 10,\n",
    "    \"mode\": \"matchFace\"#matchPerson\n",
    "    }\n",
    "\n",
    "r_findsimilars = requests.post(findsimilars_url,headers=headers,json=data_findsimilars)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_findsimilars.status_code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'persistedFaceId': '6c27182b-fabd-4a8f-a477-3a8185194a5b',\n",
       "  'confidence': 0.1868},\n",
       " {'persistedFaceId': '585b5548-2693-44f8-a8b2-2bc177fe5ba6',\n",
       "  'confidence': 0.17084},\n",
       " {'persistedFaceId': 'd9a0952f-9991-4196-950e-55c337b5838b',\n",
       "  'confidence': 0.11171},\n",
       " {'persistedFaceId': 'ceff8e23-e304-4b96-bea0-2f33adfa1b62',\n",
       "  'confidence': 0.1017},\n",
       " {'persistedFaceId': '6245988d-0e6c-4307-949c-3b4e42a9f23b',\n",
       "  'confidence': 0.10114},\n",
       " {'persistedFaceId': 'feec5a9a-e7d4-46c5-94cb-28377778d697',\n",
       "  'confidence': 0.0995},\n",
       " {'persistedFaceId': 'b4e21332-4ac6-46e9-bce9-7cd8eb81fd98',\n",
       "  'confidence': 0.09931},\n",
       " {'persistedFaceId': '27be3057-d252-4294-98ea-d55471c519d3',\n",
       "  'confidence': 0.09893},\n",
       " {'persistedFaceId': '7addfab0-e8b4-4ae7-bfea-0d0b0908feba',\n",
       "  'confidence': 0.09871},\n",
       " {'persistedFaceId': '41b0c22b-ea49-418a-9d89-4661d771de0a',\n",
       "  'confidence': 0.09574}]"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_findsimilars.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 做报表\n",
    "> 做到这里其实你已经成功了，但是为了能够直观的吧数据显示出来，我们一起来做一个报表吧！"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 将人脸数据库做成表格"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd #别忘了导入我们的伙伴pandas哦"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'persistedFaces': [{'persistedFaceId': '598f6ecd-049e-45bf-916b-e011e79700e7',\n",
       "   'userData': '丘小峰'},\n",
       "  {'persistedFaceId': '2f658068-ef61-4708-b91c-d05d4e1e0cc5',\n",
       "   'userData': '郑梓轩'},\n",
       "  {'persistedFaceId': 'ceff8e23-e304-4b96-bea0-2f33adfa1b62',\n",
       "   'userData': '郑晓萍'},\n",
       "  {'persistedFaceId': 'b41a0ba7-e29c-46e6-8c27-02ac6b1ddbfe',\n",
       "   'userData': '丘天惠'},\n",
       "  {'persistedFaceId': '7addfab0-e8b4-4ae7-bfea-0d0b0908feba',\n",
       "   'userData': '林嘉茵'},\n",
       "  {'persistedFaceId': '76c3c12f-a25d-439c-879e-9a8f43a29370',\n",
       "   'userData': '汤玲萍'},\n",
       "  {'persistedFaceId': '7bbdc1b8-6322-4a9a-9246-5049f0cd24e0',\n",
       "   'userData': '曾雯燕'},\n",
       "  {'persistedFaceId': '5bd52697-3745-44f7-9907-090d871c9692',\n",
       "   'userData': '谢依希'},\n",
       "  {'persistedFaceId': 'b6638cb0-83a1-41d0-9e8a-8dd3ce2f7dbb',\n",
       "   'userData': '杨悦聪'},\n",
       "  {'persistedFaceId': '41b0c22b-ea49-418a-9d89-4661d771de0a',\n",
       "   'userData': '周雨'},\n",
       "  {'persistedFaceId': 'caf93389-bd39-43aa-8ea8-a43797cb99d1',\n",
       "   'userData': '刘瑜鹏'},\n",
       "  {'persistedFaceId': '6245988d-0e6c-4307-949c-3b4e42a9f23b',\n",
       "   'userData': '陈嘉仪'},\n",
       "  {'persistedFaceId': 'b4e21332-4ac6-46e9-bce9-7cd8eb81fd98',\n",
       "   'userData': '徐旖芊'},\n",
       "  {'persistedFaceId': '6c27182b-fabd-4a8f-a477-3a8185194a5b',\n",
       "   'userData': '刘心如'},\n",
       "  {'persistedFaceId': 'b58ac686-aa4c-445a-8114-95a7cc044e10',\n",
       "   'userData': '刘宇'},\n",
       "  {'persistedFaceId': '585b5548-2693-44f8-a8b2-2bc177fe5ba6',\n",
       "   'userData': '李婷'},\n",
       "  {'persistedFaceId': 'feec5a9a-e7d4-46c5-94cb-28377778d697',\n",
       "   'userData': '黄智毅'},\n",
       "  {'persistedFaceId': '50d1ff59-4e82-488d-b878-10549d1b97a8',\n",
       "   'userData': '黄慧文'},\n",
       "  {'persistedFaceId': 'ea130ca7-7817-46d0-a5eb-0625e69bb214',\n",
       "   'userData': '张铭睿'},\n",
       "  {'persistedFaceId': '166c9164-add0-45ed-93f3-591f9d5d261a',\n",
       "   'userData': '陈婷'},\n",
       "  {'persistedFaceId': 'd9a0952f-9991-4196-950e-55c337b5838b',\n",
       "   'userData': '洪可凡'},\n",
       "  {'persistedFaceId': 'd66b3761-dc9b-4dfb-a19a-aaf06c450a24',\n",
       "   'userData': '卢继志'},\n",
       "  {'persistedFaceId': '27be3057-d252-4294-98ea-d55471c519d3',\n",
       "   'userData': '张梓乐'}],\n",
       " 'faceListId': 'who_you',\n",
       " 'name': 'list_pm_c',\n",
       " 'userData': '我的同学们'}"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "get_facelist_url = \"https://api-hjq.cognitiveservices.azure.com/face/v1.0/facelists/{faceListId}\"\n",
    "\n",
    "data = {\n",
    "    \"faceListId\":\"who_you\",\n",
    "\n",
    "}\n",
    "\n",
    "r_get_facelist = requests.get(get_facelist_url,headers=headers,params=data)\n",
    "r_get_facelist.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>persistedFaceId</th>\n",
       "      <th>userData</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>598f6ecd-049e-45bf-916b-e011e79700e7</td>\n",
       "      <td>丘小峰</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2f658068-ef61-4708-b91c-d05d4e1e0cc5</td>\n",
       "      <td>郑梓轩</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>ceff8e23-e304-4b96-bea0-2f33adfa1b62</td>\n",
       "      <td>郑晓萍</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>b41a0ba7-e29c-46e6-8c27-02ac6b1ddbfe</td>\n",
       "      <td>丘天惠</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>7addfab0-e8b4-4ae7-bfea-0d0b0908feba</td>\n",
       "      <td>林嘉茵</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>76c3c12f-a25d-439c-879e-9a8f43a29370</td>\n",
       "      <td>汤玲萍</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>7bbdc1b8-6322-4a9a-9246-5049f0cd24e0</td>\n",
       "      <td>曾雯燕</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>5bd52697-3745-44f7-9907-090d871c9692</td>\n",
       "      <td>谢依希</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>b6638cb0-83a1-41d0-9e8a-8dd3ce2f7dbb</td>\n",
       "      <td>杨悦聪</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>41b0c22b-ea49-418a-9d89-4661d771de0a</td>\n",
       "      <td>周雨</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>caf93389-bd39-43aa-8ea8-a43797cb99d1</td>\n",
       "      <td>刘瑜鹏</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>6245988d-0e6c-4307-949c-3b4e42a9f23b</td>\n",
       "      <td>陈嘉仪</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>b4e21332-4ac6-46e9-bce9-7cd8eb81fd98</td>\n",
       "      <td>徐旖芊</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>6c27182b-fabd-4a8f-a477-3a8185194a5b</td>\n",
       "      <td>刘心如</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>b58ac686-aa4c-445a-8114-95a7cc044e10</td>\n",
       "      <td>刘宇</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>585b5548-2693-44f8-a8b2-2bc177fe5ba6</td>\n",
       "      <td>李婷</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>feec5a9a-e7d4-46c5-94cb-28377778d697</td>\n",
       "      <td>黄智毅</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>50d1ff59-4e82-488d-b878-10549d1b97a8</td>\n",
       "      <td>黄慧文</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>ea130ca7-7817-46d0-a5eb-0625e69bb214</td>\n",
       "      <td>张铭睿</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>166c9164-add0-45ed-93f3-591f9d5d261a</td>\n",
       "      <td>陈婷</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>d9a0952f-9991-4196-950e-55c337b5838b</td>\n",
       "      <td>洪可凡</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>d66b3761-dc9b-4dfb-a19a-aaf06c450a24</td>\n",
       "      <td>卢继志</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>27be3057-d252-4294-98ea-d55471c519d3</td>\n",
       "      <td>张梓乐</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                         persistedFaceId userData\n",
       "0   598f6ecd-049e-45bf-916b-e011e79700e7      丘小峰\n",
       "1   2f658068-ef61-4708-b91c-d05d4e1e0cc5      郑梓轩\n",
       "2   ceff8e23-e304-4b96-bea0-2f33adfa1b62      郑晓萍\n",
       "3   b41a0ba7-e29c-46e6-8c27-02ac6b1ddbfe      丘天惠\n",
       "4   7addfab0-e8b4-4ae7-bfea-0d0b0908feba      林嘉茵\n",
       "5   76c3c12f-a25d-439c-879e-9a8f43a29370      汤玲萍\n",
       "6   7bbdc1b8-6322-4a9a-9246-5049f0cd24e0      曾雯燕\n",
       "7   5bd52697-3745-44f7-9907-090d871c9692      谢依希\n",
       "8   b6638cb0-83a1-41d0-9e8a-8dd3ce2f7dbb      杨悦聪\n",
       "9   41b0c22b-ea49-418a-9d89-4661d771de0a       周雨\n",
       "10  caf93389-bd39-43aa-8ea8-a43797cb99d1      刘瑜鹏\n",
       "11  6245988d-0e6c-4307-949c-3b4e42a9f23b      陈嘉仪\n",
       "12  b4e21332-4ac6-46e9-bce9-7cd8eb81fd98      徐旖芊\n",
       "13  6c27182b-fabd-4a8f-a477-3a8185194a5b      刘心如\n",
       "14  b58ac686-aa4c-445a-8114-95a7cc044e10       刘宇\n",
       "15  585b5548-2693-44f8-a8b2-2bc177fe5ba6       李婷\n",
       "16  feec5a9a-e7d4-46c5-94cb-28377778d697      黄智毅\n",
       "17  50d1ff59-4e82-488d-b878-10549d1b97a8      黄慧文\n",
       "18  ea130ca7-7817-46d0-a5eb-0625e69bb214      张铭睿\n",
       "19  166c9164-add0-45ed-93f3-591f9d5d261a       陈婷\n",
       "20  d9a0952f-9991-4196-950e-55c337b5838b      洪可凡\n",
       "21  d66b3761-dc9b-4dfb-a19a-aaf06c450a24      卢继志\n",
       "22  27be3057-d252-4294-98ea-d55471c519d3      张梓乐"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "adf = pd.json_normalize(r_get_facelist.json()[\"persistedFaces\"])# 升级pandas才能运行\n",
    "adf"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 将最终结果相似度数据做成表格"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>persistedFaceId</th>\n",
       "      <th>confidence</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>6c27182b-fabd-4a8f-a477-3a8185194a5b</td>\n",
       "      <td>0.18680</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>585b5548-2693-44f8-a8b2-2bc177fe5ba6</td>\n",
       "      <td>0.17084</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>d9a0952f-9991-4196-950e-55c337b5838b</td>\n",
       "      <td>0.11171</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>ceff8e23-e304-4b96-bea0-2f33adfa1b62</td>\n",
       "      <td>0.10170</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>6245988d-0e6c-4307-949c-3b4e42a9f23b</td>\n",
       "      <td>0.10114</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>feec5a9a-e7d4-46c5-94cb-28377778d697</td>\n",
       "      <td>0.09950</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>b4e21332-4ac6-46e9-bce9-7cd8eb81fd98</td>\n",
       "      <td>0.09931</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>27be3057-d252-4294-98ea-d55471c519d3</td>\n",
       "      <td>0.09893</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>7addfab0-e8b4-4ae7-bfea-0d0b0908feba</td>\n",
       "      <td>0.09871</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>41b0c22b-ea49-418a-9d89-4661d771de0a</td>\n",
       "      <td>0.09574</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                        persistedFaceId  confidence\n",
       "0  6c27182b-fabd-4a8f-a477-3a8185194a5b     0.18680\n",
       "1  585b5548-2693-44f8-a8b2-2bc177fe5ba6     0.17084\n",
       "2  d9a0952f-9991-4196-950e-55c337b5838b     0.11171\n",
       "3  ceff8e23-e304-4b96-bea0-2f33adfa1b62     0.10170\n",
       "4  6245988d-0e6c-4307-949c-3b4e42a9f23b     0.10114\n",
       "5  feec5a9a-e7d4-46c5-94cb-28377778d697     0.09950\n",
       "6  b4e21332-4ac6-46e9-bce9-7cd8eb81fd98     0.09931\n",
       "7  27be3057-d252-4294-98ea-d55471c519d3     0.09893\n",
       "8  7addfab0-e8b4-4ae7-bfea-0d0b0908feba     0.09871\n",
       "9  41b0c22b-ea49-418a-9d89-4661d771de0a     0.09574"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bdf = pd.json_normalize(r_findsimilars.json())# 升级pandas才能运行\n",
    "bdf"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 完成！合并成报表！看看谁跟我长的最像\n",
    "> [pandas命令速查](https://pandas.pydata.org/Pandas_Cheat_Sheet.pdf)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>persistedFaceId</th>\n",
       "      <th>userData</th>\n",
       "      <th>confidence</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>6c27182b-fabd-4a8f-a477-3a8185194a5b</td>\n",
       "      <td>刘心如</td>\n",
       "      <td>0.18680</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>585b5548-2693-44f8-a8b2-2bc177fe5ba6</td>\n",
       "      <td>李婷</td>\n",
       "      <td>0.17084</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>d9a0952f-9991-4196-950e-55c337b5838b</td>\n",
       "      <td>洪可凡</td>\n",
       "      <td>0.11171</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>ceff8e23-e304-4b96-bea0-2f33adfa1b62</td>\n",
       "      <td>郑晓萍</td>\n",
       "      <td>0.10170</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>6245988d-0e6c-4307-949c-3b4e42a9f23b</td>\n",
       "      <td>陈嘉仪</td>\n",
       "      <td>0.10114</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>feec5a9a-e7d4-46c5-94cb-28377778d697</td>\n",
       "      <td>黄智毅</td>\n",
       "      <td>0.09950</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>b4e21332-4ac6-46e9-bce9-7cd8eb81fd98</td>\n",
       "      <td>徐旖芊</td>\n",
       "      <td>0.09931</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>27be3057-d252-4294-98ea-d55471c519d3</td>\n",
       "      <td>张梓乐</td>\n",
       "      <td>0.09893</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>7addfab0-e8b4-4ae7-bfea-0d0b0908feba</td>\n",
       "      <td>林嘉茵</td>\n",
       "      <td>0.09871</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>41b0c22b-ea49-418a-9d89-4661d771de0a</td>\n",
       "      <td>周雨</td>\n",
       "      <td>0.09574</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                        persistedFaceId userData  confidence\n",
       "5  6c27182b-fabd-4a8f-a477-3a8185194a5b      刘心如     0.18680\n",
       "6  585b5548-2693-44f8-a8b2-2bc177fe5ba6       李婷     0.17084\n",
       "8  d9a0952f-9991-4196-950e-55c337b5838b      洪可凡     0.11171\n",
       "0  ceff8e23-e304-4b96-bea0-2f33adfa1b62      郑晓萍     0.10170\n",
       "3  6245988d-0e6c-4307-949c-3b4e42a9f23b      陈嘉仪     0.10114\n",
       "7  feec5a9a-e7d4-46c5-94cb-28377778d697      黄智毅     0.09950\n",
       "4  b4e21332-4ac6-46e9-bce9-7cd8eb81fd98      徐旖芊     0.09931\n",
       "9  27be3057-d252-4294-98ea-d55471c519d3      张梓乐     0.09893\n",
       "1  7addfab0-e8b4-4ae7-bfea-0d0b0908feba      林嘉茵     0.09871\n",
       "2  41b0c22b-ea49-418a-9d89-4661d771de0a       周雨     0.09574"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.merge(adf, bdf,how='inner', on='persistedFaceId').sort_values(by=\"confidence\",ascending = False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![黄杰琪](http://huangjieqi.gitee.io/picture_storage/hjq.jpg)\n",
    "![刘心如](http://huangjieqi.gitee.io/picture_storage/liuxinrujiayou.jpg)\n",
    "![李婷](http://huangjieqi.gitee.io/picture_storage/ltco.jpg)\n",
    "![洪可凡](http://huangjieqi.gitee.io/picture_storage/coco022.jpg)\n",
    "![陈嘉仪](http://huangjieqi.gitee.io/picture_storage/jiayichen.jpg)\n",
    "![黄智毅](http://huangjieqi.gitee.io/picture_storage/lucaszy.jpg)\n",
    "![徐旖芊](http://huangjieqi.gitee.io/picture_storage/kg2000.jpg)\n",
    "![张梓乐](http://huangjieqi.gitee.io/picture_storage/zzlhyy.jpg)\n",
    "![林嘉茵](http://huangjieqi.gitee.io/picture_storage/L-Tony-info.jpg)\n",
    "![周雨](http://huangjieqi.gitee.io/picture_storage/Zoezhouyu.jpg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "307.2px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
